package com.zh.zuul.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 过滤拦截器
 * @Author: Li.HuiNeng
 * @Create in: 2019/8/8
 * @Version: 1.0.1
 */


@Component
public class LoginFilter extends ZuulFilter {


    private static final int LOGIN_ORDER = 11;

    private Logger log = LoggerFactory.getLogger(LoginFilter.class);


    /**
     * @Description:配置过滤类型，有四种不同生命周期的过滤器类型
     * 1.pre：路由之前
     * 2.routing：路由之时
     * 3.post：路由之后
     * 4.error：发送错误调用
     * ------------------------
     * @Author:Li.HuiNeng
     * @Date:2019/8/8
     */


    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return LOGIN_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * @Description: 此拦截器做接口拦截(登录接口, 注册接口, 注销接口和刷新token接口放行)
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/8
     */

    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        //System.out.println(request.getServletPath());
        String[] paths = {
                "/auth/login/",
                "/fhb/getToken",
                "/auth/oauth/check_token",
                "/auth/authClient/refreshToken",
                "/auth/authManage/",
                "/risk/call"
        };
        for (int i = 0; i < paths.length; i++) {
            //System.err.println(request.getServletPath());
            // 接口放行
            if (request.getServletPath().contains(paths[i])) return null;
        }
        //向header中添加鉴权令牌
        RequestContext requestContext = RequestContext.getCurrentContext();
        //获取header
        String authorization = request.getHeader("Authorization");
        if (authorization != null) {
            requestContext.addZuulRequestHeader("Authorization", authorization);
        } else {
            log.warn("Token is empty");
            context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("data", "null");
                jsonObject.put("message", "Token is empty");
                jsonObject.put("code", "401");
                jsonObject.put("success", "false");
                context.getResponse().getWriter().write(jsonObject.toJSONString());
            } catch (IOException e) {

            }
        }
        return null;
    }

}
